package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleDao roleDao;
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //利用mybatis的分页助手插件进行分页查询
        PageHelper.startPage(currentPage, pageSize);
        //根据条件进行分页查询
        Page<Role> page=roleDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total, rows);
    }
    //添加角色
    @Override
    public void add(Role role, Integer[] menuIds,Integer[] permissionIds) {

        //向角色中添加
        roleDao.add(role);
        //向中间表设置角色和菜单权限信息
        setRoleAndMenu(role.getId(), menuIds);
        setRoleAndPermission(role.getId(),permissionIds);
    }
    //向中间表中加入菜单和角色的关联关系
    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
    //向中间表中加入权限和角色的关联关系
    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
    //回显数据role
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }
    //编辑
    @Override
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        //编辑角色(即基本信息)
        roleDao.updateRole(role);
        //删除关联菜单和权限信息
        roleDao.deleteRoleAndMenu(role.getId());
        roleDao.deleteRoleAndPermission(role.getId());
        //重新将关联关系加入中间表中
        setRoleAndPermission(role.getId(), permissionIds);
        setRoleAndMenu(role.getId(),menuIds);
    }
    //删除角色
    @Override
    public void deleteRole(Integer id) {
        //删除中间表
        roleDao.deleteRoleAndMenu(id);
        roleDao.deleteRoleAndPermission(id);
        //删除角色信息
        roleDao.deleteRole(id);

    }
}
